package mall.kotlin.com.myapplication.view.animator;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.BounceInterpolator;
import android.widget.Button;

import mall.kotlin.com.myapplication.R;

public class AnimatorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        final Button button = findViewById(R.id.button);

        //valueAnimator 动画获取具体的值,设置给view的属性  手动修改view的属性
        //button.setOnClickListener(new View.OnClickListener() {
        //    @Override
        //    public void onClick(View v) {
        //        ValueAnimator valueAnimator = ValueAnimator.ofInt(button.getLayoutParams().width, 600);
        //        valueAnimator.setDuration(1000);
        //        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        //            @Override
        //            public void onAnimationUpdate(ValueAnimator animation) {
        //                int current = (int) animation.getAnimatedValue();
        //                Log.i("current", current + "");
        //                button.getLayoutParams().width = current;
        //                button.requestLayout();
        //            }
        //        });
        //        valueAnimator.start();
        //    }
        //});

        //设置方法2：在XML 代码中设置,在路径 res/animator的文件夹里创建相应的动画.xml文件
        Animator animator = AnimatorInflater.loadAnimator(this,  R.animator.set_animation);
        animator.setTarget(button);
        animator.setDuration(1000);
        animator.start();
        //--------------------------------------
        //objectAnimator   自动修改view的属性
        float curTranslationX = button.getTranslationX();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(button,"alpha",1f,0f,1f); //透明度
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(button,"rotation",0f,360f);//旋转
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(button,"translationX",curTranslationX,360f,curTranslationX);//p平移
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(button, "scaleX", 1f, 3f, 1f);//缩放
        objectAnimator.setDuration(2000);
        objectAnimator.start();

        //AnimatorSet
        //AnimatorSet.play(Animator anim)   ：播放当前动画
        //AnimatorSet.after(long delay)   ：将现有动画延迟x毫秒后执行
        //AnimatorSet.with(Animator anim)   ：将现有动画和传入的动画同时执行
        //AnimatorSet.after(Animator anim)   ：将现有动画插入到传入的动画之后执行
        //AnimatorSet.before(Animator anim) ：  将现有动画插入到传入的动画之前执行
        // 步骤1：设置需要组合的动画效果
        ObjectAnimator translation = ObjectAnimator.ofFloat(button, "translationX", curTranslationX, 300,curTranslationX);
        // 平移动画
        ObjectAnimator rotate = ObjectAnimator.ofFloat(button, "rotation", 0f, 360f);
        // 旋转动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(button, "alpha", 1f, 0f, 1f);
        // 透明度动画

        // 步骤2：创建组合动画的对象
        AnimatorSet animSet = new AnimatorSet();

        // 步骤3：根据需求组合动画
        animSet.play(translation).with(rotate).before(alpha);
        animSet.setDuration(5000);

        // 步骤4：启动动画
        animSet.start();

        // 单个动画设置:将按钮变成透明状态
        button.animate().alpha(0f);
        // 单个动画效果设置 & 参数设置
        button.animate().alpha(0f).setDuration(5000).setInterpolator(new BounceInterpolator());
        // 组合动画:将按钮变成透明状态再移动到(500,500)处
        button.animate().alpha(0f).x(500).y(500);



    }
}
